/**
 * Created by Administrator on 2019/4/1.
 */
var roleList = null;
var editUserForm = null;
$(function () {
    editUserForm = $("#editUserForm");
    roleList = postSyncAjax("/selectRolesForCombobox/normal",null);
    initCombobox();
    restoreFunc();
    saveFunc();
});

var initCombobox = function () {
    $("#status1").combobox({
        editable : false, // 不可编辑状态
        cache : false,
        panelHeight : 'auto',// 自动高度适合
        valueField : 'statusVal',
        textField : 'statusName'
    });
    var status1 = [];
    status1.push({
        "statusVal":0,
        "statusName":"停用"
    });
    status1.push({
        "statusVal":1,
        "statusName":"启用"
    });
    $("#status1").combobox("clear");
    $("#status1").combobox("loadData", status1);
    var oldStatus = $("#oldStatus").val();
    if(null != oldStatus && undefined != oldStatus && "" != oldStatus) {
        $('#status1').combobox('setValue', oldStatus);
    }
    var oldDepartmentId = $("#oldDepartmentId").val();
    if(null != oldDepartmentId && undefined != oldDepartmentId && "" != oldDepartmentId) {
        $('#departmentId').combobox('setValue', oldDepartmentId);
    }

    $("#rId").combobox({
        editable : false, // 不可编辑状态
        cache : false,
        panelHeight : 'auto',// 自动高度适合
        valueField : 'roleId',
        textField : 'roleName'
    });
    var roles = [];
    for(var i=0; i < roleList.length; i++) {
        roles.push({
            "roleId": roleList[i].id,
            "roleName":roleList[i].roleName
        });
    }
    console.log("测试："+roles);
    $("#rId").combobox("clear");
    $("#rId").combobox("loadData", roles);
    var oldRoleId = $("#oldRoleId").val();
    if(null != oldRoleId && undefined != oldRoleId && "" != oldRoleId) {
        $("#rId").combobox("setValue",oldRoleId);
    }
}

var saveFunc = function () {
    $("#save").click(function () {
        if(editUserForm.form("enableValidation")) {
            editUserForm.form('submit', {
                url:'/user/editCurrentUser/normal',
                success:function (result) {
                    var r = $.parseJSON(result);
                    if(r.success) {
                        $.messager.alert("消息提示","编辑成功！","info");
                    } else {
                        if (result.status == 401) {
                            $.messager.alert("消息提示",r.msg,"warning",
                                function () {
                                    window.top.location.href = "/index";
                                });
                        }
                        $.messager.alert("消息提示","编辑失败：" + r.msg,"warning");
                        return;
                    }
                }
            });
        }
    });
}
var restoreFunc = function () {
    $("#restore").click(function () {
        editUserForm.form("reset");
        editUserForm.form("disableValidation");
    });
}